<template>
  <template 
  v-for="(item, index) in props.menuData" :key="`${index}-${item.meta?.id }`">
    <el-menu-item
    @click="handleClick(item)"
      v-if="!item.children || item.children.length === 0"
      :index="item.meta?.path || item.path">
      <el-icon size="20" v-if="item.meta?.icon">
        <component :is="item.meta.icon"/>
      </el-icon>
      <span>{{ item.meta?.name || item.path }}</span>
    </el-menu-item>
    
    <el-sub-menu 
      v-else
      :index="item.meta?.path || item.path">
      <template #title>
        <el-icon size="20" v-if="item.meta?.icon">
          <component :is="item.meta.icon"/>
        </el-icon>
        <span>{{ item.meta?.name }}</span>
      </template>
      <TreeMenu :menu-data="item.children" />
    </el-sub-menu>
  </template>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'

const router = useRouter()
const store = useStore()

const handleClick = (item) => {
  store.commit('addMenu', item.meta)
  store.commit('updateMenuActive',item.meta?.path || item.path)
  router.push(item.meta.path)
}


const props = defineProps({
  menuData: {
    type: Array,
    required: true
  }
})
</script>

